package net.example.activity.adapter;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.oraycn.esbasic.adapters.CommonAdapter;

import net.example.activity.GroupDetailActivity;
import net.example.activity.adapter.interfaces.IHandleOnClickCallback;
import net.example.manager.Manager;
import net.example.model.GroupNotifyModel;
import net.example.ggtalk.R;
import net.example.utils.IntentUtils;

import java.util.List;

public class GroupNotifyAdpter extends CommonAdapter<GroupNotifyModel>
{
    private IHandleOnClickCallback<GroupNotifyModel> handleOnClickCallback;
    public GroupNotifyAdpter(Context context, List<GroupNotifyModel> datas, int item_layoutId)
    {
        super(context,datas,item_layoutId);
    }

    /*
     * 设置同意、拒绝按钮的回调处理
     * */
    public void setHandleOnClickCallback(IHandleOnClickCallback<GroupNotifyModel> callback)
    {
        this.handleOnClickCallback=callback;
    }

    @Override
    protected void setConvertView(View convertView, final GroupNotifyModel model, int itemViewType) {
        try {
            ViewHolder holder;
            if (convertView.getTag() == null) {
                holder = new ViewHolder(convertView);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.headImg.setImageResource(R.drawable.group_ico);
            holder.headImg.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击跳转到用户详情界面
                    IntentUtils.startActivityForString(context, GroupDetailActivity.class, "requestID", model.AddGroupRequest.GroupID);
                }
            });
            holder.title_left.setText(model.IsRequester ? "":model.Requester.getCommentName());
            if(model.IsRequester)
            {
                holder.title_left.setVisibility(View.GONE);
            }else {
                holder.title_left.setVisibility(View.VISIBLE);
                holder.title_left.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Manager.getInstance().moveToUserDetail(context,model.Requester.getUserID());
                    }
                });
            }

            holder.userName.setText(model.IsRequester ? "已申请加入":"申请加入" );
            holder.title_right.setText(model.Group.getName());
            holder.title_right.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //点击跳转到用户详情界面
                    IntentUtils.startActivityForString(context, GroupDetailActivity.class, "requestID", model.AddGroupRequest.GroupID);
                }
            });
            if (model.IsRequester) {
                holder.describe.setText("已发送验证请求");
            } else {
                holder.describe.setText(model.AddGroupRequest.Comment);
            }
            if (!model.IsRequester && model.AddGroupRequest.State == 0) {
                holder.operateResult.setVisibility(View.GONE);
                holder.operatePanle.setVisibility(View.VISIBLE);
                holder.btn_agree.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (handleOnClickCallback != null) {
                            handleOnClickCallback.onAgreeClick(model);
                        }
                    }
                });
                holder.btn_refuse.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (handleOnClickCallback != null) {
                            handleOnClickCallback.onRefuseClick(model);
                        }
                    }
                });
            } else {
                holder.operatePanle.setVisibility(View.GONE);
                holder.operateResult.setVisibility(View.VISIBLE);
                String resultStr = "";
                if (model.AddGroupRequest.State == 0) {
                    resultStr = "等待验证";
                } else if (model.AddGroupRequest.State == 1) {
                    resultStr = "已同意";
                } else if (model.AddGroupRequest.State == 2) {
                    resultStr = "已拒绝";
                }
                holder.operateResult.setText(resultStr);
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    private class ViewHolder
    {
        public ImageView headImg;
        public TextView userName;
        public TextView title_left;
        public TextView title_right;
        public TextView describe;
        public Button btn_refuse;
        public Button btn_agree;
        public View operatePanle;
        public TextView operateResult;

        public ViewHolder(View view)
        {
            this.headImg = (ImageView) view.findViewById(R.id.ct_photo);
            this.userName=(TextView) view.findViewById(R.id.ct_name);
            this.userName.setTextSize(12);
            this.title_left=(TextView)view.findViewById(R.id.title_left);
            this.title_right=(TextView)view.findViewById(R.id.title_right);
            this.describe=(TextView) view.findViewById(R.id.tv_describe);
            this.btn_refuse=(Button)view.findViewById(R.id.btn_refuse);
            this.btn_agree=(Button)view.findViewById(R.id.btn_agree);
            this.operatePanle=(View)view.findViewById(R.id.operatePanle);
            this.operateResult=(TextView)view.findViewById(R.id.operateResult);
        }
    }
}
